package com.spa.infrastructure.repository.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spa.infrastructure.repository.condition.ShopWithdrawRecordCondition;
import com.spa.infrastructure.repository.mapper.ShopWithdrawRecordMapper;
import com.spa.infrastructure.repository.po.ShopWithdrawRecordPO;
import com.spa.infrastructure.util.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@Slf4j
public class ShopWithdrawRecordRepositoryService extends ServiceImpl<ShopWithdrawRecordMapper, ShopWithdrawRecordPO> {

    @Autowired
    private ShopWithdrawRecordMapper shopWithdrawRecordMapper;

    public void add(ShopWithdrawRecordPO po) {
        shopWithdrawRecordMapper.insert(po);
    }

    public ShopWithdrawRecordPO getShopWithdrawRecordById(long id) {
        return getById(id);
    }

    public Page<ShopWithdrawRecordPO> page(ShopWithdrawRecordCondition condition){
        return page(Page.of(condition.getCurrent(), condition.getSize()), wrapper(condition));
    }

    public List<ShopWithdrawRecordPO> list(ShopWithdrawRecordCondition condition){
        return list(wrapper(condition));
    }

    public void updateShopWithdrawRecord(ShopWithdrawRecordPO po) {
        update(null, updateWrapper(po));
    }

    private LambdaQueryWrapper<ShopWithdrawRecordPO> wrapper(ShopWithdrawRecordCondition condition) {
        LambdaQueryWrapper<ShopWithdrawRecordPO> wrapper = Wrappers.lambdaQuery();
        wrapper.ge(StringUtils.isNotEmpty(condition.getBeginTime()), ShopWithdrawRecordPO::getApplyTime, condition.getBeginTime());
        wrapper.le(StringUtils.isNotEmpty(condition.getEndTime()), ShopWithdrawRecordPO::getApplyTime, condition.getEndTime());
        wrapper.eq(ObjectUtil.isNotNull(condition.getShopId()), ShopWithdrawRecordPO::getShopId, condition.getShopId());
        wrapper.eq(ObjectUtil.isNotNull(condition.getStatus()), ShopWithdrawRecordPO::getStatus, condition.getStatus());
        wrapper.in(CollectionUtil.isNotEmpty(condition.getIds()), ShopWithdrawRecordPO::getId, condition.getIds());
        wrapper.orderByDesc(ShopWithdrawRecordPO::getApplyTime);
        return wrapper;
    }

    private LambdaUpdateWrapper<ShopWithdrawRecordPO> updateWrapper(ShopWithdrawRecordPO po) {
        LambdaUpdateWrapper<ShopWithdrawRecordPO> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(ShopWithdrawRecordPO::getId, po.getId());
        updateWrapper.set(ObjectUtil.isNotNull(po.getStatus()), ShopWithdrawRecordPO::getStatus, po.getStatus());
        updateWrapper.set(ObjectUtil.isNotNull(po.getAuditTime()), ShopWithdrawRecordPO::getAuditTime, po.getAuditTime());
        updateWrapper.set(ObjectUtil.isNotNull(po.getPayTime()), ShopWithdrawRecordPO::getPayTime, po.getPayTime());
        updateWrapper.set(ObjectUtil.isNotNull(po.getAuditName()), ShopWithdrawRecordPO::getAuditName, po.getAuditName());
        updateWrapper.set(ObjectUtil.isNotNull(po.getAuditId()), ShopWithdrawRecordPO::getAuditId, po.getAuditId());
        updateWrapper.set(StringUtils.isNotEmpty(po.getCredentials()), ShopWithdrawRecordPO::getCredentials, po.getCredentials());
        return updateWrapper;
    }
}
